// Changes the name of the last swatch
if ( app.documents.length > 0 && app.activeDocument.swatches.length > 0 ) {
  var docRef = app.activeDocument
  var isRGBSpace = _getColorSpace() === 'RGB'

  var jettaRgbColor = {
    'jetta blue': [40, 90, 255],
    'eosin': [255, 20, 60],
    'iron gray': [45, 62, 78],
    'Sky blue': [153, 212, 255],
    'silver gray': [189, 194, 195]
  }
  var jettaCMYKColor = {
    'jetta blue': [90, 55, 0, 0],
    'eosin': [0, 95, 75, 0],
    'iron gray': [40, 15, 0, 75],
    'Sky blue': [35, 5, 0, 0],
    'silver gray': [0, 0, 0, 25]
  }
  var colors = isRGBSpace ? jettaRgbColor : jettaCMYKColor;
  clearSwatchGroupByName('Jetta Swacth')
  createSwatchGroup('Jetta Swacth', colors)
}

function _getColorSpace() {
  return app.activeDocument.documentColorSpace == DocumentColorSpace.CMYK ? 'CMYK' : 'RGB'
}

function clearSwatchGroupByName(swatchName) {
  var swatchGroups = app.activeDocument.swatchGroups

  for (var i=0; i < swatchGroups.length ; i++) {
    if(swatchName === swatchGroups[i].name) {
      swatchGroups[i].remove()
    }
  };
}

function createSwatchGroup(swatchGroupName, colors) {
  var swatchGroup = app.activeDocument.swatchGroups.add();
  swatchGroup.name = swatchGroupName;
  for(var key in colors) {
    var newSwatch = _genRgbSwatch(key, colors[key])
    swatchGroup.addSwatch(newSwatch)
  }
}

function _genRgbColor(color) {
  var newRGBColor = new RGBColor();
  newRGBColor.red = color[0];
  newRGBColor.green = color[1];
  newRGBColor.blue = color[2];
  return newRGBColor
}

function _genCmykColor(color) {
  var newCMYKColor = new CMYKColor();
  newCMYKColor.cyan = color[0];
  newCMYKColor.magenta = color[1];
  newCMYKColor.yellow = color[2];
  newCMYKColor.black = color[3];
  return newCMYKColor
}

function _genRgbSwatch(name, color) {
  var newSwatch = app.activeDocument.swatches.add();
  newSwatch.color = isRGBSpace ? _genRgbColor(color) : _genCmykColor(color);
  newSwatch.name = name;
  return newSwatch
}
